package com.umakr.ax.app.service;

import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.collect.Maps;
import com.umakr.ax.common.mapper.InteractiveLotteryMapper;
import com.umakr.ax.common.mapper.UserDetailsMapper;
import com.umakr.ax.common.model.*;
import com.umakr.ax.common.service.LotteryResultService;
import com.umakr.ax.core.base.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 用户抽奖实现类
 * </p>
 *
 * @author zj
 * @date 2018/5/7
 */
@Service
public class InteractiveLotteryWebServices extends BaseService<InteractiveLotteryMapper, InteractiveLottery>{

    @Autowired
    LotteryResultService lotteryResultService;

    @Autowired
    UserDetailsMapper userDetailsMapper;

    public Object listLotteryHistory(Long rsId,String lotteryName) {
        if("".equals(lotteryName)) {
            lotteryName = null;
        }
        Page<Map<String,Object>> page = baseMapper.listLotteryHistory(rsId,lotteryName);
        for(Map<String,Object> map : page.getRecords()){
            Object lotteryStatus = map.get("lotteryStatus");
            Object informStatus = map.get("informStatus");
            Object messageFrom = map.get("messageFrom");
            if((Integer.parseInt(lotteryStatus.toString())) == 0){
                map.put("lotteryStatus","未抽");
            }else{
                map.put("lotteryStatus","已抽");
            }
            if((Integer.parseInt(informStatus.toString())) == 0){
                map.put("informStatus","未通知");
            }else if((Integer.parseInt(informStatus.toString())) == 1){
                map.put("informStatus","部分通知");
            }else {
                map.put("informStatus","全部通知");
            }
            if(messageFrom != null){
                if((Integer.parseInt(messageFrom.toString())) == 0){
                    map.put("messageFrom","微信");
                }else {
                    map.put("messageFrom","app");
                }
            } else{
                map.put("messageFrom","全部");
            }
        }
        return page;
    }

    public Object saveOrUpdateLottery(InteractiveLottery interactiveLottery) {
        int errorCode = 0 ;
        String msg = "发布失败";
        Map<String,Object> data = Maps.newLinkedHashMap();
        Date date = new Date();
        if(interactiveLottery.getId() == null){
            interactiveLottery.setCreatTime(date);
            LotteryResult userInfo = userDetailsMapper.queryUserMessageById(interactiveLottery.getCreateUserId());
            interactiveLottery.setCreateUserName(userInfo.getNickname());
            baseMapper.saveLottery(interactiveLottery);
            msg = "新增成功";
        } else {
            baseMapper.updateLottery(interactiveLottery);
            msg = "修改成功";
        }
        data.put("id",interactiveLottery.getId());
        data.put("lotteryName",interactiveLottery.getLotteryName());
        data.put("startTime",interactiveLottery.getStartTime());
        data.put("endTime",interactiveLottery.getEndTime());
        data.put("peopleCount",interactiveLottery.getPeopleCount());
        data.put("keyWord",interactiveLottery.getKeyWord());
        data.put("creatTime",interactiveLottery.getCreatTime());
        data.put("messageFrom",interactiveLottery.getMessageFrom());
        errorCode = 1;
        return  resultMap(data,errorCode,msg);
    }

    public Object deleteLottery(Long lotteryId) {
        int errorCode = 0 ;
        String msg = "操作失败";
        baseMapper.deleteLottery(lotteryId);
        errorCode = 1 ;
        msg = "删除成功";
        return resultMap(errorCode,msg);
    }

    public Object getLotteryInfoById(Long lotteryId) {
        InteractiveLottery lottery = queryById(lotteryId);
        Object lotteryResult = lotteryResultService.queryByLotteryId(lotteryId);
        Map<String,Object> map = Maps.newHashMap();
        if(lottery != null) {
            map.put("lotteryName",lottery.getLotteryName());
            map.put("startTime",lottery.getStartTime());
            map.put("endTime",lottery.getEndTime());
            map.put("peopleCount",lottery.getPeopleCount());
            map.put("keyWord",lottery.getKeyWord());
            map.put("lotteryResultData",lotteryResult);
        }
        return resultMap(map);
    }
}
